using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Pipes;
using System.Security.Principal;
using System.Threading;

namespace PipeHitter
{

    public class PipeClient 
    {
        private string pipename;
      

        public PipeClient(string pipename)
        {

            this.pipename = pipename;
        }

        public string SendMessage(string message, int timeout = 500)
        {
            var resp = new StringBuilder();
            var util = new PipeStreamUtil();
            using (var pipe = new NamedPipeClientStream(this.pipename))
            {
                try
                {
                    try
                    {
                        pipe.Connect(timeout);
                    }
                    catch (TimeoutException ex)
                    {
                        throw new TimeoutException("Could not connect to pipe " + this.pipename, ex);
                    }
                    util.WriteString(pipe, message);
                    pipe.WaitForPipeDrain();
                    return util.ReadString(pipe);
                }
                finally
                {
                    pipe.Close();
                }
            }
        }

    }
}




